package com.android.delivery;

import android.Manifest;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.android.delivery.activity.LoginActivity;
import com.android.delivery.api.ApiService;
import com.android.delivery.base.BaseActivity;
import com.android.delivery.callback.DialogCallback;
import com.android.delivery.callback.LzyResponse;
import com.android.delivery.constant.SputilsConstant;
import com.android.delivery.dialog.AgreementDialog;
import com.android.delivery.entity.LoginInfo;
import com.android.delivery.entity.User;
import com.android.delivery.util.SoundUtil;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.lzy.okgo.model.Response;
import com.tbruyelle.rxpermissions3.RxPermissions;

/**
 * Created by mqwang on 2017/9/28.
 */

public class SplashActivity extends BaseActivity {

    String[] perms = {
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
//            Manifest.permission.ACCESS_BACKGROUND_LOCATION, //target为Q时，动态请求后台定位权限
            Manifest.permission.READ_EXTERNAL_STORAGE
    };

    private Handler mHandler = new Handler();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        setStatusBarTintResource(R.color.transparent);
        //初始化通知铃声
        SoundUtil.getInstance().init();
        LoginInfo loginInfo = App.Companion.getInstance().getLoginInfo();
        if (loginInfo != null){
            ApiService.user_info(this, new DialogCallback<LzyResponse<User>>() {
                @Override
                public void onSuccess(Response<LzyResponse<User>> response) {
                    User user = response.body().data;
                    if (user != null){
                        loginInfo.getUser().setAvatar(user.getAvatar());
                        loginInfo.getUser().setNickname(user.getNickname());
                    }
                }
            });
        }

        boolean showAgree = SPUtils.getInstance().getBoolean(SputilsConstant.SHOW_AGREE_DIALOG, true);
        if (showAgree){
            AgreementDialog agreementDialog = AgreementDialog.newInstance();
            agreementDialog.setAgreementDialogListener(new AgreementDialog.AgreementDialogListener() {
                @Override
                public void onAgree() {
                    SPUtils.getInstance().put(SputilsConstant.SHOW_AGREE_DIALOG, false);
                    turnMain(0);
                }

                @Override
                public void onDisAgree() {
                    App.Companion.getInstance().exitApp();
                }
            });
            agreementDialog.show(getSupportFragmentManager());
        }else {
            turnMain(3000);
        }
    }

    private void turnMain(long delay) {
        RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity or Fragment instance
        rxPermissions
                .request(perms)
                .subscribe(granted -> {
                    jumpMain(delay);
                });
    }

    private void jumpMain(long delay) {
        if (mHandler != null){
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    LoginInfo loginInfo = App.Companion.getInstance().getLoginInfo();
                    if (loginInfo == null) {
                        ActivityUtils.startActivity(SplashActivity.this, LoginActivity.class);
                    } else {
                        ActivityUtils.startActivity(SplashActivity.this, MainActivity.class);
                    }
                    finish();

                }
            }, delay);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mHandler != null){
            mHandler.removeCallbacksAndMessages(null);
        }
    }

    @Override
    protected boolean isNeedTitleBar() {
        return false;
    }
}
